package com.toyota.toyotaownerspoc.util;

import java.io.ByteArrayOutputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageUtility {

	/*
	 * calculateInSampleSize () loads a scaled down version of the image into
	 * memory.
	 * http://developer.android.com/training/displaying-bitmaps/load-bitmap
	 * .html#load-bitmap
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {

		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 4;
		if (height > reqHeight || width > reqWidth) {
			final int halfHeight = height / 2;
			final int halfWidth = width / 2;

			// Calculate the largest inSampleSize value that is a power of 2 and
			// keeps both
			// height and width larger than the requested height and width.
			while ((halfHeight / inSampleSize) > reqHeight
					&& (halfWidth / inSampleSize) > reqWidth) {
				inSampleSize *= 2;
			}
		}
		return inSampleSize;
	}// end calculateInSampleSize

	/*
	 * decodeSampledBitmapFromResource() based on desired height & weight from
	 * image on storag esuch a son the SD card
	 * http://developer.android.com/training
	 * /displaying-bitmaps/load-bitmap.html#load-bitmap
	 */
	public static Bitmap decodeSampledBitmapFromResource(String pathName,
			int reqWidth, int reqHeight) {
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		// Decode bitmap with inSampleSize set options.inJustDecodeBounds =
		// false;
		return BitmapFactory.decodeFile(pathName, options);

	}

	/*
	 * Get the The resulting width of the bitmap of the image resource
	 */

	public static int getImageResourceWidth(Resources res, int resId) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		//
		return options.outWidth;
	}

	/*
	 * Get the The resulting width of the bitmap of the image resource
	 */

	public static int getImageResourceHeight(Resources res, int resId) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);

		return options.outHeight;
	}
	
	/*Used to convert Bitmap images to Byte [] array
	 * 
	 */
	@SuppressWarnings("finally")
	public static byte[] bitmapToByteArray(Bitmap bitmap) {
		ByteArrayOutputStream baoStream = null;
		try {
			baoStream = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baoStream);
		} catch (Exception exc) {
			Log.d("ClassScanViewBarcodeActivity, bitmapToByteArray (Bitmap bitmap) Exception: ",
					exc.getMessage());
		} finally {
			return baoStream.toByteArray();
		}
	}
}
